import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";

/**
 * model - 系统消息、平台公告卡片
 * @date: 2019-08-28
 */
import { getResponse } from 'utils/utils';
import { changeRead, queryAnnouncement, queryUserMessage } from "../../services/cards/messageService";
export default {
  namespace: 'messageCard',
  state: {
    systemMessageList: [],
    // 系统消息
    announcementList: [],
    // 公告消息
    notificationList: [] // 系统通知

  },
  effects: {
    // #region cpaas
    // 查询系统消息
    cpaasQueryUserMessage: /*#__PURE__*/_regeneratorRuntime.mark(function cpaasQueryUserMessage(_ref, _ref2) {
      var payload, call, put, data, userMessageTypeCode, obj;
      return _regeneratorRuntime.wrap(function cpaasQueryUserMessage$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.t0 = getResponse;
              _context.next = 5;
              return call(queryUserMessage, payload);

            case 5:
              _context.t1 = _context.sent;
              data = (0, _context.t0)(_context.t1);

              if (!data) {
                _context.next = 12;
                break;
              }

              userMessageTypeCode = payload.userMessageTypeCode;
              obj = userMessageTypeCode === 'MSG' ? {
                systemMessageList: data.content
              } : {
                notificationList: data.content
              };
              _context.next = 12;
              return put({
                type: 'updateState',
                payload: _objectSpread({}, obj)
              });

            case 12:
              return _context.abrupt("return", data);

            case 13:
            case "end":
              return _context.stop();
          }
        }
      }, cpaasQueryUserMessage);
    }),
    // 查询平台公告
    cpaasQueryAnnouncement: /*#__PURE__*/_regeneratorRuntime.mark(function cpaasQueryAnnouncement(_ref3, _ref4) {
      var payload, call, put, data;
      return _regeneratorRuntime.wrap(function cpaasQueryAnnouncement$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(queryAnnouncement, payload);

            case 5:
              _context2.t1 = _context2.sent;
              data = (0, _context2.t0)(_context2.t1);

              if (!data) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  announcementList: data.content
                }
              });

            case 10:
              return _context2.abrupt("return", data);

            case 11:
            case "end":
              return _context2.stop();
          }
        }
      }, cpaasQueryAnnouncement);
    }),
    // 系统消息变为已读
    cpaasChangeRead: /*#__PURE__*/_regeneratorRuntime.mark(function cpaasChangeRead(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function cpaasChangeRead$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(changeRead, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, cpaasChangeRead);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref7) {
      var payload = _ref7.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};